<template>
	<gradient-background>
		<view class="container">
			<view class="content">
				<!-- 页面标题 -->
				<view class="page-header">
					<view class="back-button" @tap="goBack">
						<text class="back-icon">←</text>
					</view>
					<view class="header-info">
						<text class="tool-icon">🚗</text>
						<text class="tool-title">出行占卜</text>
						<text class="tool-subtitle">出行吉凶和方位指导</text>
					</view>
				</view>
				
				<!-- 参数输入区域 -->
				<app-card>
					<view class="input-section">
						<text class="section-title">出行信息</text>
						
						<view class="form-item">
							<text class="form-label">出发地</text>
							<input 
								v-model="params.departure" 
								placeholder="请输入出发地"
								class="input-field"
							/>
						</view>
						
						<view class="form-item">
							<text class="form-label">目的地</text>
							<input 
								v-model="params.destination" 
								placeholder="请输入目的地"
								class="input-field"
							/>
						</view>
						
						<view class="form-item">
							<text class="form-label">出行日期</text>
							<picker 
								mode="date" 
								:value="params.date" 
								@change="onDateChange"
								class="date-picker"
							>
								<view class="picker-display">
									<text class="picker-text">{{ displayDate }}</text>
									<text class="picker-arrow">→</text>
								</view>
							</picker>
						</view>
						
						<view class="form-item">
							<text class="form-label">出行时间</text>
							<picker 
								mode="time" 
								:value="params.time" 
								@change="onTimeChange"
								class="time-picker"
							>
								<view class="picker-display">
									<text class="picker-text">{{ displayTime }}</text>
									<text class="picker-arrow">→</text>
								</view>
							</picker>
						</view>
						
						<view class="form-item">
							<text class="form-label">出行方式</text>
							<view class="travel-methods">
								<view 
									v-for="method in travelMethods" 
									:key="method.value"
									class="method-item"
									:class="{ active: params.method === method.value }"
									@tap="selectMethod(method.value)"
								>
									<text class="method-icon">{{ method.icon }}</text>
									<text class="method-name">{{ method.name }}</text>
								</view>
							</view>
						</view>
					</view>
				</app-card>
				
				<!-- 查询按钮 -->
				<view class="action-section">
					<app-button 
						:text="loading ? '占卜中...' : '开始占卜'" 
						type="primary"
						@tap="queryTravel"
						:disabled="!canQuery || loading"
						:loading="loading"
					></app-button>
				</view>
				
				<!-- 结果显示区域 -->
				<app-card v-if="travelResult">
					<view class="result-section">
						<text class="section-title">出行分析</text>
						
						<view class="travel-analysis">
							<view class="overall-result">
								<view class="result-icon">
									<text class="icon-text">{{ getResultIcon(travelResult.overall.level) }}</text>
								</view>
								<view class="result-info">
									<text class="result-level">{{ travelResult.overall.level }}</text>
									<text class="result-desc">{{ travelResult.overall.description }}</text>
								</view>
							</view>
							
							<view class="travel-details">
								<view class="detail-item">
									<text class="detail-label">最佳时辰</text>
									<text class="detail-value">{{ travelResult.bestTime }}</text>
								</view>
								<view class="detail-item">
									<text class="detail-label">吉利方位</text>
									<text class="detail-value">{{ travelResult.luckyDirection }}</text>
								</view>
								<view class="detail-item">
									<text class="detail-label">注意事项</text>
									<text class="detail-value">{{ travelResult.attention }}</text>
								</view>
							</view>
						</view>
					</view>
				</app-card>
				
				<!-- 方位指导 -->
				<app-card v-if="travelResult && travelResult.direction">
					<view class="direction-section">
						<text class="section-title">方位指导</text>
						
						<view class="direction-display">
							<view class="compass">
								<view class="compass-center">
									<text class="center-text">出行</text>
								</view>
								<view 
									v-for="dir in directions" 
									:key="dir.name"
									class="direction-item"
									:class="{ lucky: travelResult.direction.lucky.includes(dir.name), 
											  avoid: travelResult.direction.avoid.includes(dir.name) }"
									:style="{ '--angle': dir.angle }"
								>
									<text class="dir-name">{{ dir.name }}</text>
								</view>
							</view>
							
							<view class="direction-legend">
								<view class="legend-item">
									<view class="legend-color lucky"></view>
									<text class="legend-text">吉利方位</text>
								</view>
								<view class="legend-item">
									<view class="legend-color avoid"></view>
									<text class="legend-text">避免方位</text>
								</view>
							</view>
							
							<view class="direction-advice">
								<text class="advice-text">{{ travelResult.direction.advice }}</text>
							</view>
						</view>
					</view>
				</app-card>
				
				<!-- 出行建议 -->
				<app-card v-if="travelResult && travelResult.suggestions">
					<view class="suggestions-section">
						<text class="section-title">出行建议</text>
						
						<view class="suggestions-list">
							<view 
								v-for="(suggestion, index) in travelResult.suggestions" 
								:key="index"
								class="suggestion-item"
							>
								<view class="suggestion-icon">
									<text class="icon-text">💡</text>
								</view>
								<text class="suggestion-text">{{ suggestion }}</text>
							</view>
						</view>
					</view>
				</app-card>
			</view>
		</view>
	</gradient-background>
</template>

<script>
import GradientBackground from '@/components/gradient-background/gradient-background.vue'
import AppCard from '@/components/app-card/app-card.vue'
import AppButton from '@/components/app-button/app-button.vue'
import ToolsService from '@/utils/tools-service.js'

export default {
	name: 'TravelToolPage',
	components: {
		GradientBackground,
		AppCard,
		AppButton
	},
	data() {
		return {
			params: {
				departure: '',
				destination: '',
				date: '',
				time: '',
				method: 'car'
			},
			travelResult: null,
			loading: false,
			toolsService: null,
			travelMethods: [
				{ value: 'car', name: '汽车', icon: '🚗' },
				{ value: 'train', name: '火车', icon: '🚄' },
				{ value: 'plane', name: '飞机', icon: '✈️' },
				{ value: 'bus', name: '客车', icon: '🚌' },
				{ value: 'walk', name: '步行', icon: '🚶' },
				{ value: 'bike', name: '骑行', icon: '🚴' }
			],
			directions: [
				{ name: '北', angle: 0 },
				{ name: '东北', angle: 45 },
				{ name: '东', angle: 90 },
				{ name: '东南', angle: 135 },
				{ name: '南', angle: 180 },
				{ name: '西南', angle: 225 },
				{ name: '西', angle: 270 },
				{ name: '西北', angle: 315 }
			]
		}
	},
	computed: {
		canQuery() {
			return this.params.departure.trim().length > 0 && 
				   this.params.destination.trim().length > 0
		},
		
		displayDate() {
			return this.params.date || '选择日期'
		},
		
		displayTime() {
			return this.params.time || '选择时间'
		}
	},
	onLoad() {
		this.initData()
	},
	methods: {
		initData() {
			this.toolsService = new ToolsService()
			
			// 设置默认日期和时间
			const now = new Date()
			this.params.date = now.toISOString().split('T')[0]
			this.params.time = now.toTimeString().slice(0, 5)
		},
		
		goBack() {
			uni.navigateBack()
		},
		
		onDateChange(e) {
			this.params.date = e.detail.value
		},
		
		onTimeChange(e) {
			this.params.time = e.detail.value
		},
		
		selectMethod(method) {
			this.params.method = method
		},
		
		async queryTravel() {
			if (!this.canQuery) {
				uni.showToast({
					title: '请填写出发地和目的地',
					icon: 'none'
				})
				return
			}
			
			this.loading = true
			
			try {
				const result = await this.toolsService.getTravelInfo(this.params)
				
				if (result.success) {
					this.travelResult = result.data
					uni.showToast({
						title: '占卜完成',
						icon: 'success'
					})
				} else {
					throw new Error(result.message || '占卜失败')
				}
			} catch (error) {
				console.error('出行占卜失败:', error)
				uni.showToast({
					title: '占卜失败，请重试',
					icon: 'none'
				})
			} finally {
				this.loading = false
			}
		},
		
		getResultIcon(level) {
			const icons = {
				'大吉': '🟢',
				'中吉': '🟡',
				'小吉': '🔵',
				'凶': '🔴',
				'大凶': '⚫'
			}
			return icons[level] || '🟡'
		}
	}
}
</script>

<style lang="scss" scoped>
.container {
	min-height: 100vh;
	padding: 32rpx;
}

.content {
	padding-top: 40rpx;
}

.page-header {
	display: flex;
	align-items: center;
	margin-bottom: 40rpx;
	
	.back-button {
		width: 80rpx;
		height: 80rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: rgba(255, 255, 255, 0.9);
		border-radius: 40rpx;
		margin-right: 24rpx;
		box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.1);
		
		.back-icon {
			font-size: 32rpx;
			color: #000000;
			font-weight: bold;
		}
	}
	
	.header-info {
		flex: 1;
		
		.tool-icon {
			font-size: 64rpx;
			margin-bottom: 8rpx;
		}
		
		.tool-title {
			display: block;
			font-size: 40rpx;
			font-weight: bold;
			color: #000000;
			margin-bottom: 8rpx;
		}
		
		.tool-subtitle {
			display: block;
			font-size: 26rpx;
			color: rgba(0, 0, 0, 0.7);
		}
	}
}

.input-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.form-item {
		margin-bottom: 32rpx;
		
		&:last-child {
			margin-bottom: 0;
		}
		
		.form-label {
			display: block;
			font-size: 28rpx;
			color: #000000;
			margin-bottom: 16rpx;
		}
		
		.input-field {
			width: 100%;
			height: 88rpx;
			padding: 0 24rpx;
			border: 2rpx solid #f0f0f0;
			border-radius: 12rpx;
			font-size: 28rpx;
			background-color: #fafafa;
		}
		
		.date-picker,
		.time-picker {
			.picker-display {
				display: flex;
				align-items: center;
				justify-content: space-between;
				height: 88rpx;
				padding: 0 24rpx;
				border: 2rpx solid #f0f0f0;
				border-radius: 12rpx;
				background-color: #fafafa;
				
				.picker-text {
					font-size: 28rpx;
					color: #000000;
				}
				
				.picker-arrow {
					font-size: 24rpx;
					color: rgba(0, 0, 0, 0.6);
				}
			}
		}
		
		.travel-methods {
			display: grid;
			grid-template-columns: repeat(3, 1fr);
			gap: 16rpx;
			
			.method-item {
				display: flex;
				flex-direction: column;
				align-items: center;
				padding: 20rpx;
				border: 2rpx solid #f0f0f0;
				border-radius: 12rpx;
				background-color: #fafafa;
				transition: all 0.3s ease;
				
				&.active {
					border-color: #7ED321;
					background: linear-gradient(135deg, #7ED321 0%, #5BA517 100%);
					
					.method-icon,
					.method-name {
						color: white;
					}
				}
				
				.method-icon {
					font-size: 32rpx;
					margin-bottom: 8rpx;
				}
				
				.method-name {
					font-size: 24rpx;
					color: #000000;
				}
			}
		}
	}
}

.action-section {
	margin: 48rpx 0;
}

.result-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.travel-analysis {
		.overall-result {
			display: flex;
			align-items: center;
			margin-bottom: 32rpx;
			padding: 24rpx;
			background: linear-gradient(135deg, #7ED321 0%, #5BA517 100%);
			border-radius: 16rpx;
			
			.result-icon {
				margin-right: 24rpx;
				
				.icon-text {
					font-size: 64rpx;
				}
			}
			
			.result-info {
				flex: 1;
				
				.result-level {
					display: block;
					font-size: 36rpx;
					font-weight: bold;
					color: white;
					margin-bottom: 8rpx;
				}
				
				.result-desc {
					display: block;
					font-size: 28rpx;
					color: rgba(255, 255, 255, 0.9);
					line-height: 1.5;
				}
			}
		}
		
		.travel-details {
			display: flex;
			flex-direction: column;
			gap: 16rpx;
			
			.detail-item {
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 20rpx;
				background-color: #f8f9fa;
				border-radius: 12rpx;
				
				.detail-label {
					font-size: 28rpx;
					color: rgba(0, 0, 0, 0.7);
				}
				
				.detail-value {
					font-size: 28rpx;
					font-weight: 500;
					color: #000000;
				}
			}
		}
	}
}

.direction-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.direction-display {
		.compass {
			position: relative;
			width: 300rpx;
			height: 300rpx;
			margin: 0 auto 32rpx;
			border: 4rpx solid #f0f0f0;
			border-radius: 50%;
			
			.compass-center {
				position: absolute;
				top: 50%;
				left: 50%;
				transform: translate(-50%, -50%);
				width: 80rpx;
				height: 80rpx;
				background: linear-gradient(135deg, #7ED321 0%, #5BA517 100%);
				border-radius: 50%;
				display: flex;
				align-items: center;
				justify-content: center;
				
				.center-text {
					font-size: 24rpx;
					color: white;
					font-weight: 500;
				}
			}
			
			.direction-item {
				position: absolute;
				width: 60rpx;
				height: 60rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				border-radius: 50%;
				background-color: #f8f9fa;
				border: 2rpx solid #e0e0e0;
				
				&.lucky {
					background: linear-gradient(135deg, #4CAF50 0%, #2E7D32 100%);
					border-color: #4CAF50;
					
					.dir-name {
						color: white;
					}
				}
				
				&.avoid {
					background: linear-gradient(135deg, #F44336 0%, #C62828 100%);
					border-color: #F44336;
					
					.dir-name {
						color: white;
					}
				}
				
				.dir-name {
					font-size: 20rpx;
					font-weight: 500;
				}
			}
		}
		
		.direction-legend {
			display: flex;
			justify-content: center;
			gap: 32rpx;
			margin-bottom: 24rpx;
			
			.legend-item {
				display: flex;
				align-items: center;
				gap: 8rpx;
				
				.legend-color {
					width: 20rpx;
					height: 20rpx;
					border-radius: 50%;
					
					&.lucky {
						background: linear-gradient(135deg, #4CAF50 0%, #2E7D32 100%);
					}
					
					&.avoid {
						background: linear-gradient(135deg, #F44336 0%, #C62828 100%);
					}
				}
				
				.legend-text {
					font-size: 24rpx;
					color: rgba(0, 0, 0, 0.7);
				}
			}
		}
		
		.direction-advice {
			padding: 20rpx;
			background-color: #e8f5e8;
			border-radius: 12rpx;
			text-align: center;
			
			.advice-text {
				font-size: 26rpx;
				color: #2e7d2e;
				line-height: 1.5;
			}
		}
	}
}

.suggestions-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.suggestions-list {
		.suggestion-item {
			display: flex;
			align-items: flex-start;
			margin-bottom: 20rpx;
			
			&:last-child {
				margin-bottom: 0;
			}
			
			.suggestion-icon {
				margin-right: 16rpx;
				margin-top: 4rpx;
				
				.icon-text {
					font-size: 32rpx;
				}
			}
			
			.suggestion-text {
				flex: 1;
				font-size: 28rpx;
				color: #000000;
				line-height: 1.6;
			}
		}
	}
}

/* 罗盘方位布局 */
.direction-item[style*="--angle: 0"] {
	top: 10rpx;
	left: 50%;
	transform: translateX(-50%);
}

.direction-item[style*="--angle: 45"] {
	top: 25rpx;
	right: 25rpx;
}

.direction-item[style*="--angle: 90"] {
	top: 50%;
	right: 10rpx;
	transform: translateY(-50%);
}

.direction-item[style*="--angle: 135"] {
	bottom: 25rpx;
	right: 25rpx;
}

.direction-item[style*="--angle: 180"] {
	bottom: 10rpx;
	left: 50%;
	transform: translateX(-50%);
}

.direction-item[style*="--angle: 225"] {
	bottom: 25rpx;
	left: 25rpx;
}

.direction-item[style*="--angle: 270"] {
	top: 50%;
	left: 10rpx;
	transform: translateY(-50%);
}

.direction-item[style*="--angle: 315"] {
	top: 25rpx;
	left: 25rpx;
}
</style>